/*
Author Dave Lenz
Creation date: 17 jun 2009
 */
package nl.ypmits.utils.as3.view.interfaces.dotmatrixtyper {
	import nl.ypmits.utils.as3.EasySetup;
	import nl.ypmits.utils.as3.view.interfaces.dotmatrixtyper.fonts.DotMatrixFont;

	import flash.display.Sprite;
	import flash.events.Event;

	public class TitleDot extends Sprite {

		private var dotradius : Number;
		private var text : String;
		private var rows : Array = [];

		
		public function TitleDot( _text : String = "<please fill in text>", _dotradius : Number = 5 ) {
			text = _text;
			dotradius = _dotradius;
			
			addEventListener( Event.ADDED_TO_STAGE, init );
		}

		
		private function init(event : Event) : void {
			removeEventListener( Event.ADDED_TO_STAGE, init );
			
			drawDots( );
			
			drawText( text );
		}

		
		private function drawDots( ) : void {
			var aantal : int = ( EasySetup.instance.stage.stageWidth / dotradius ) + 2;
			for (var i : int = 0; i < 9 ; i++) {
				var row : Array = [];
				for (var j : int = 0; j < aantal ; j++) {
					var dot : Dot = new Dot( dotradius );
					dot.name = "row" + i + " column" + j;
					dot.x = j * dotradius;
					dot.y = i * dotradius;
					dot.deactivate( );
					addChild( dot );
					
					row.push( dot );
				}
				rows.push( row );
			}
		}

		
		private function drawText( text : String ) : void {
			for (var j : int = 0; j < 8 ; j++) {
				var row : Array = [];
				for (var i : int = 0; i < text.length ; i++) {
					var letter : TitleDotLetter = TitleDotLetter( DotMatrixFont[text.substr( i, 1 )] );
					var na : Array = row.concat( letter.array[j], 0 ); // laatste nul is een spatie
					row = na;
				}
				for (var k : int = 0; k < row.length ; k++) {
					try {
						if( row[k] == 1) {
							Dot( rows[j][k] ).activate( );
						} else if( row[k] == 0 ) {
							Dot( rows[j][k] ).deactivate( );
						}
					} catch( e : * ) {
					}
				}
			}
		}
	}
}
